﻿package com.wandian.util
{
	import com.wandian.data.GameData;
	
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;

	/**
	* @author long
	* @QQ 108232706
	* @version 1.0
	*/
	public class ImageLoaderSprite extends Sprite
	{
		public var loader:Loader;
		private var _w:Number;
		private var _h:Number;
		private var _url:String;

		public function ImageLoaderSprite()
		{
			return;
		}

		public function load(url:String, w:Number=0, h:Number=0):void
		{
			if (url == "" || url == "0")
			{
				return;
			}
			try
			{
				this._w=w;
				this._h=h;
				this._url=url;
				this.loader=new Loader();
				this.loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, this.progressFun);
				this.loader.contentLoaderInfo.addEventListener(Event.COMPLETE, this.imageDataComplete);
				this.loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, this.onError);
				this.loader.load(new URLRequest(url));
			}
			catch (e:Error)
			{
			}
			return;
		}

		private function progressFun(event:ProgressEvent):void
		{
			dispatchEvent(event);
			return;
		}

		private function onError(event:IOErrorEvent):void
		{
			trace(this._url+" error");
			return;
		}

		private function imageDataComplete(event:Event):void
		{
			var mc:MovieClip;
			var e:*=event;
			try
			{
				trace(this._url+" ok");
				if (this.loader.content != null)
				{
					mc=this.loader.content as MovieClip;
					if (mc != null)
					{
						this.addChild(mc);
					}
					else
					{
						try
						{
							this.addChild(this.loader.content);
						}
						catch (e:Error)
						{
							this.addChild(loader);
						}
					}
				}
				else
				{
					this.addChild(this.loader);
				}
			}
			catch (e:Error)
			{
				this.addChild(loader);
			}
			dispatchEvent(e);
			return;
		}

	}
}
